/*
 * CodeSaveCommand.java
 *
 * Created on 2006년 3월 29일 (수), 오후 4:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.welfare.admin.system;

import java.io.File;
import java.util.*;





import javax.servlet.*;
import javax.servlet.http.*;

import com.zll.framework.conf.Configuration;
import com.zll.framework.model.HashBox;
import com.zll.framework.servlet.Command;
import com.zll.framework.util.StringUtil;
import com.welfare.common.CommonUtil;





/**
 *
 * @author eastar
 */
public class DocumentSaveCommand implements Command
{

    public String execute(HttpServletRequest  request, HttpServletResponse response)
    throws Exception
    {
    	SystemDAO dao 		= new SystemDAO();
    	
    	
    	HashBox box = new HashBox(request);

    	String docuId = box.get("docu_id");
    	
    	//신규등록
    	if ("".equals(docuId)) {
    		docuId = dao.selectDocumentId(new HashMap());
    		box.put("docu_id", docuId);
    	}
    	
    	//첨부파일
		File f = box.getFile("file_path");
		if (f != null) {
			
			String fileId = CommonUtil.getFileId();
			String fileExt = box.getFileExt("file_path").toLowerCase();
			
			File newFile = new File(Configuration.getHomePath() + "/docu/" + fileId + "." + fileExt);
			f.renameTo(newFile);
			
			box.put("file_id", fileId);
			box.put("file_name", box.getFileName("file_path"));
			box.put("file_ext", fileExt);
			box.put("file_path", "/docu/" + newFile.getName());
		}

		
		dao.saveDocument(box);


		HashMap result		= new HashMap();
		result.put("result", 	"true");
		result.put("target",	"_opener");
		result.put("action",	"./documentList.jsp");

    	request.setAttribute("result", result);


    	return "/common/move_view.jsp";
    }
}
